$(function () {
    $("#search").on("click", function () {
        var keyword = $("#keyword").val().trim();
        if (keyword == null || keyword == "") {
            alert("请输入抓取的关键字");
            return;
        }
        var totalTime = 0;
        var t = setInterval(function () {
            $("#hit").text("正在抓取，请稍候！" + (totalTime++) + " 秒").fadeOut(1000).fadeIn(1000);
            $("#hit").css("color", "red");
        }, 1000);
        $.ajax({
            url: "/grasp/fetch/" + keyword,
            dataType: "json",
            success: function (datas) {
                console.log(datas);
                clearInterval(t);
                $("#hit").text("抓取结束！" + totalTime + " 秒.").fadeIn(1000);
                $("#hit").css("color", "green");

                if (datas.code = 200) {
                    $("#dataTBody").empty();
                    var result = datas.data;
                    for (var i = 0; i < result.length; i++) {
                        var depth = result[i].depth;
                        var pageNum = result[i].pageNum;
                        var title = result[i].title;
                        var url = result[i].url;
                        var tr_html = "<tr>" +
                            "<th>" + (i + 1) + "</th>" +
                            // "<th>" + depth + "</th>" +
                            // "<th>" + pageNum + "</th>" +
                            // "<th>" + title + "</th>" +
                            "<th><a href=' " + url + "' target='_blank'>" + title + "</a></th>" +
                            "<th></th>" +
                            "</tr>";
                        $("#dataTBody").append(tr_html);
                    }
                } else {
                    alert(data.message);
                }
            },
            error: function () {

            }
        });
    });
});